package com.ruoyi.user.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.user.domain.QbUser;
import com.ruoyi.user.domain.QbUserWx;
import com.ruoyi.user.mapper.QbUserMapper;
import com.ruoyi.user.mapper.QbUserWxMapper;
import com.ruoyi.user.service.IQbUserWxService;
import com.ruoyi.user.util.QbDataUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 人员-微信绑定关系Service业务层处理
 *
 * @author lzhj
 * @date 2021-01-19
 */
@Service
public class QbUserWxServiceImpl implements IQbUserWxService {
    @Autowired
    private QbUserWxMapper qbUserWxMapper;
    @Autowired
    private QbUserMapper qbUserMapper;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    JdbcTemplate jdbcTemplate;


    /**
     * 查询人员-微信绑定关系
     *
     * @param id 人员-微信绑定关系ID
     * @return 人员-微信绑定关系
     */
    @Override
    public QbUserWx selectQbUserWxById(Long id) {
        return qbUserWxMapper.selectQbUserWxById(id);
    }

    /**
     * 查询人员-微信绑定关系列表
     *
     * @param qbUserWx 人员-微信绑定关系
     * @return 人员-微信绑定关系
     */
    @Override
    public List<QbUserWx> selectQbUserWxList(QbUserWx qbUserWx) {
        return qbUserWxMapper.selectQbUserWxList(qbUserWx);
    }

    /**
     * 新增人员-微信绑定关系
     *
     * @param qbUserWx 人员-微信绑定关系
     * @return 结果
     */
    @Override
    public int insertQbUserWx(QbUserWx qbUserWx) {
        qbUserWx.setCreateTime(DateUtils.getNowDate());
        return qbUserWxMapper.insertQbUserWx(qbUserWx);
    }

    /**
     * 修改人员-微信绑定关系
     *
     * @param qbUserWx 人员-微信绑定关系
     * @return 结果
     */
    @Override
    public int updateQbUserWx(QbUserWx qbUserWx) {
        qbUserWx.setUpdateTime(DateUtils.getNowDate());
        return qbUserWxMapper.updateQbUserWx(qbUserWx);
    }

    /**
     * 批量删除人员-微信绑定关系
     *
     * @param ids 需要删除的人员-微信绑定关系ID
     * @return 结果
     */
    @Override
    public int deleteQbUserWxByIds(Long[] ids) {
        int n = 0;
        for (Long id : ids) {
            int i = deleteQbUserWxById(id);
            n += i;
        }
        return n;
//        return qbUserWxMapper.deleteQbUserWxByIds(ids);
    }

    /**
     * 删除人员-微信绑定关系信息
     *
     * @param id 人员-微信绑定关系ID
     * @return 结果
     */
    @Override
    public int deleteQbUserWxById(Long id) {
        QbUserWx qbUserWx = qbUserWxMapper.selectQbUserWxById(id);
        int i = qbUserWxMapper.deleteQbUserWxById(id);
        if (qbUserWx != null) {
            String openId = qbUserWx.getOpenId();
            List<QbUser> qbUserList = qbUserMapper.queryUserListByOpenid(openId);
            //设置微信绑定的用户的缓存
            redisCache.setCacheObject(QbDataUtil.getWxUserCacheKey(openId), qbUserList);
        }

        return i;
    }

    @Override
    public int unBindWx(Long id) {
        return qbUserWxMapper.deleteQbUserWxByUserid(new Long[]{id});
    }


    @Override
    public Boolean isBindWx(Long userId) {
        String sql = "select count(1) from t_qb_user_wx where user_id=?";
        Integer i = jdbcTemplate.queryForObject(sql, Integer.class, userId);
        return i > 0 ? true : false;
    }

    @Override
    public QbUserWx getQbUserWxByUserIdAndWx(Long userId, String openId) {
        QbUserWx qbUser1 = qbUserWxMapper.getQbUserWxByUserIdAndWx(userId, openId);
        return qbUser1;
    }

    @Override
    public Boolean validataUserInfo(Long userId, String openId) {
        Boolean b = false;
        //获取微信绑定的用户的缓存
        List<QbUser> qbUsers = redisCache.getCacheObject(QbDataUtil.getWxUserCacheKey(openId));
//        if (CollectionUtils.isEmpty(qbUsers)) {
//            qbUsers = qbUserMapper.queryUserListByOpenid(openId);
//            //设置微信绑定的用户的缓存
//            redisCache.setCacheObject(QbDataUtil.getWxUserCacheKey(openId), qbUsers);
//        }
        if (CollectionUtils.isNotEmpty(qbUsers)) {
            for (QbUser qbUser : qbUsers) {
                if (userId.equals(qbUser.getId())) {
                    b = true;
                    break;
                }
            }

        }
        return b;
    }

    @Override
    public List<Map<String, Object>> getNotice() {
        return qbUserWxMapper.getNotice();
    }

}
